package com.bktech.stupidnews.controlpanel;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bktech.stupidnews.MainActivity;
import com.bktech.stupidnews.R;
import com.bktech.stupidnews.controlpanel.Category.TYPE;

public class AddPanel extends Fragment {

	MainActivity activity;
	ChildCategory child;
	MyAddListAdapter adapter;
	ArrayList<Category> data;
	OnItemClickListener onItemClickListener;
	Button btnCreate;
	EditText editText;
	TextWatcher textWatcher;
	OnClickListener onClickListener;

	public AddPanel(MainActivity mactivity, ArrayList<Category> data,
			String feedId, String title, String urlImage) {
		this.activity = mactivity;
		child = new ChildCategory(feedId, title, urlImage);
		this.data = data;
		adapter = new MyAddListAdapter(activity, data);
		onItemClickListener = new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				adapter.data.get(position).add(child);
				adapter.notifyDataSetChanged();
				activity.isNewPage = false;
				activity.closeSettingPanel();
			}
		};

		textWatcher = new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				// Log.d("OnChange", "on "+s +" "+start+" "+before+" "+count);
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

				// Log.d("OnChange", "before "+s +" "+start+" "+" "+count);
			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if (s.length() == 0) {
					btnCreate.setEnabled(false);
				} else {
					btnCreate.setEnabled(true);
				}
			}
		};

		onClickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				addCategory();
			}
		};
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.setting_panel_add, container, false);
		ListView listView = (ListView) layout.findViewById(R.id.list_category);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(onItemClickListener);

		editText = (EditText) layout.findViewById(R.id.edit_text);
		editText.addTextChangedListener(textWatcher);

		btnCreate = (Button) layout.findViewById(R.id.create_button);
		btnCreate.setOnClickListener(onClickListener);

		TextView textview = (TextView) layout.findViewById(R.id.title_new_feed);
		textview.setText("Add " + child.title);
		return layout;
	}

	public void addCategory() {
		String s = editText.getText().toString();
		s = s.replace(" ", "");
		Log.d("TETES", "|" + s + "|");

		if (s.equals("")) {
			Toast.makeText(AddPanel.this.activity,
					"The name of category at least 1 character !",
					Toast.LENGTH_SHORT).show();
			return;
		}
		StringBuilder builder = new StringBuilder();
		boolean endCheck = false;
		for (int i = 0; i < s.length(); i++) {
			if (endCheck) {
				if (s.charAt(i) != ' ') {
					endCheck = true;
					builder.append(s.charAt(i));
				}
			} else {
				builder.append(s.charAt(i));
			}
		}
		Category newCategory = new Category(TYPE.CATEGORY, builder.toString());
		newCategory.add(child);
		adapter.data.add(newCategory);
		adapter.notifyDataSetChanged();
		activity.isNewPage = false;
		activity.closeSettingPanel();
	}
}
